﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using nkstdgia.common.ThamDinhGia.BusinessObjects;
using nkstdgia.common.ThamDinhGia.DataAccess;

namespace nkstdgia.common.ThamDinhGia.DataLayer
{
    public class GIA_DOAN_DUONGDA
    {
        #region ***** Init Methods *****
        public GIA_DOAN_DUONGDA()
        {
        }
        #endregion

        #region ***** Get Methods *****
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public GIA_DOAN_DUONG Populate(IDataReader myReader)
		{
			GIA_DOAN_DUONG obj = new GIA_DOAN_DUONG();
			obj.MA_GDD = (Int64) myReader["MA_GDD"];
			obj.MA_DKS = (Int64) myReader["MA_DKS"];
            obj.GIA_DAT = myReader["GIA_DAT"] != DBNull.Value ? (double)myReader["GIA_DAT"] : 0.0; 
            obj.NGUOI_KHOI_TAO = myReader["NGUOI_KHOI_TAO"] != DBNull.Value ? (Guid)myReader["NGUOI_KHOI_TAO"] : new Guid();
            obj.NGAY_KHOI_TAO = myReader["NGAY_KHOI_TAO"] != DBNull.Value ? (DateTime)myReader["NGAY_KHOI_TAO"] : new DateTime();
            obj.NGAY_CAP_NHAT = myReader["NGAY_CAP_NHAT"] != DBNull.Value ? (DateTime)myReader["NGAY_CAP_NHAT"] : new DateTime();
            obj.ACTIVE_FLAG = myReader["ACTIVE_FLAG"] != DBNull.Value ? (string)myReader["ACTIVE_FLAG"] : "N";
			return obj;
		}

        /// <summary>
        /// Get GIA_DOAN_DUONG by ma_gdd
        /// </summary>
        /// <param name="ma_gdd">MA_GDD</param>
        /// <returns>GIA_DOAN_DUONG</returns>
        public GIA_DOAN_DUONG GetByMA_GDD(Int64 ma_gdd)
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_DOAN_DUONG_GetByMA_GDD", Data.CreateParameter("MA_GDD", ma_gdd)))
            {
                if (reader.Read())
                {
                    return Populate(reader);
                }
                return null;
            }
        }

        /// <summary>
        /// Get all of GIA_DOAN_DUONG
        /// </summary>
        /// <returns>List<<GIA_DOAN_DUONG>></returns>
        public List<GIA_DOAN_DUONG> GetList()
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_DOAN_DUONG_Get"))
            {
                List<GIA_DOAN_DUONG> list = new List<GIA_DOAN_DUONG>();
                while (reader.Read())
                {
                    list.Add(Populate(reader));
                }
                return list;
            }
        }

        /// <summary>
        /// Get DataSet of GIA_DOAN_DUONG
        /// </summary>
        /// <returns>DataSet</returns>
        public DataSet GetDataSet()
        {
            return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_DOAN_DUONG_Get");
        }


        /// <summary>
        /// Get all of GIA_DOAN_DUONG paged
        /// </summary>
        /// <param name="recperpage">record per page</param>
        /// <param name="pageindex">page index</param>
        /// <returns>List<<GIA_DOAN_DUONG>></returns>
        public List<GIA_DOAN_DUONG> GetListPaged(int recperpage, int pageindex)
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_DOAN_DUONG_GetPaged"
                            , Data.CreateParameter("recperpage", recperpage)
                            , Data.CreateParameter("pageindex", pageindex)))
            {
                List<GIA_DOAN_DUONG> list = new List<GIA_DOAN_DUONG>();
                while (reader.Read())
                {
                    list.Add(Populate(reader));
                }
                return list;
            }
        }

        /// <summary>
        /// Get DataSet of GIA_DOAN_DUONG paged
        /// </summary>
        /// <param name="recperpage">record per page</param>
        /// <param name="pageindex">page index</param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSetPaged(int recperpage, int pageindex)
        {
            return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_DOAN_DUONG_GetPaged"
                            , Data.CreateParameter("recperpage", recperpage)
                            , Data.CreateParameter("pageindex", pageindex));
        }


        /// <summary>
        /// Get all of GIA_DOAN_DUONG by MA_GDD
        /// </summary>
        /// <param name="ma_gdd">ma_gdd</param>
        /// <returns>List<GIA_DOAN_DUONG></returns>
        public List<GIA_DOAN_DUONG> GetListByMA_GDD(Int64 ma_gdd)
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_DOAN_DUONG_GetByMA_GDD", Data.CreateParameter("MA_GDD", ma_gdd)))
            {
                List<GIA_DOAN_DUONG> list = new List<GIA_DOAN_DUONG>();
                while (reader.Read())
                {
                    list.Add(Populate(reader));
                }
                return list;
            }
        }

        /// <summary>
        /// Get DataSet of GIA_DOAN_DUONG by MA_GDD
        /// </summary>
        /// <param name="ma_gdd">ma_gdd</param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSetByMA_GDD(Int64 ma_gdd)
        {
            return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_DOAN_DUONG_GetByMA_GDD", Data.CreateParameter("MA_GDD", ma_gdd));
        }


        /// <summary>
        /// Get all of GIA_DOAN_DUONG by MA_GDD paged
        /// </summary>
        /// <param name="ma_gdd">MA_GDD</param>
        /// <param name="recperpage">record per page</param>
        /// <param name="pageindex">page index</param>
        /// <returns>List<<GIA_DOAN_DUONG>></returns>
        public List<GIA_DOAN_DUONG> GetListByMA_GDDPaged(Int64 ma_gdd, int recperpage, int pageindex)
        {
            using (IDataReader reader = SqlHelper.ExecuteReader(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_DOAN_DUONG_GetByMA_GDDPaged"
                            , Data.CreateParameter("recperpage", recperpage)
                            , Data.CreateParameter("pageindex", pageindex)
                            , Data.CreateParameter("MA_GDD", ma_gdd)))
            {
                List<GIA_DOAN_DUONG> list = new List<GIA_DOAN_DUONG>();
                while (reader.Read())
                {
                    list.Add(Populate(reader));
                }
                return list;
            }
        }

        /// <summary>
        /// Get DataSet of GIA_DOAN_DUONG by MA_GDD paged
        /// </summary>
        /// <param name="ma_gdd">MA_GDD</param>
        /// <param name="recperpage">record per page</param>
        /// <param name="pageindex">page index</param>
        /// <returns>DataSet</returns>
        public DataSet GetDataSetByMA_GDDPaged(Int64 ma_gdd, int recperpage, int pageindex)
        {
            return SqlHelper.ExecuteDataSet(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_DOAN_DUONG_GetByMA_GDDPaged",
                            Data.CreateParameter("recperpage", recperpage),
                            Data.CreateParameter("pageindex", pageindex),
                            Data.CreateParameter("MA_GDD", ma_gdd));
        }

        #endregion

        #region ***** Add Update Delete Methods *****
        /// <summary>
        /// Add a new GIA_DOAN_DUONG within GIA_DOAN_DUONG database table
        /// </summary>
        /// <param name="obj">GIA_DOAN_DUONG</param>
        /// <returns>key of table</returns>
        public long Add(GIA_DOAN_DUONG obj)
        {
            DbParameter parameterItemID = Data.CreateParameter("MA_GDD", obj.MA_GDD);
            parameterItemID.Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_DOAN_DUONG_Add"
                            , parameterItemID
                            , Data.CreateParameter("MA_DKS", obj.MA_DKS)
                            , Data.CreateParameter("GIA_DAT", obj.GIA_DAT)
                            , Data.CreateParameter("NGUOI_KHOI_TAO", obj.NGUOI_KHOI_TAO)
                            , Data.CreateParameter("NGAY_KHOI_TAO", obj.NGAY_KHOI_TAO)
                            , Data.CreateParameter("NGAY_CAP_NHAT", obj.NGAY_CAP_NHAT)
                            , Data.CreateParameter("ACTIVE_FLAG", obj.ACTIVE_FLAG)
            );
            return (long)parameterItemID.Value;
        }

        /// <summary>
        /// updates the specified GIA_DOAN_DUONG
        /// </summary>
        /// <param name="obj">GIA_DOAN_DUONG</param>
        /// <returns></returns>
        public void Update(GIA_DOAN_DUONG obj)
        {
            SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_DOAN_DUONG_Update"
                            , Data.CreateParameter("MA_GDD", obj.MA_GDD)
                            , Data.CreateParameter("MA_DKS", obj.MA_DKS)
                            , Data.CreateParameter("GIA_DAT", obj.GIA_DAT)
                            , Data.CreateParameter("NGUOI_KHOI_TAO", obj.NGUOI_KHOI_TAO)
                            , Data.CreateParameter("NGAY_KHOI_TAO", obj.NGAY_KHOI_TAO)
                            , Data.CreateParameter("NGAY_CAP_NHAT", obj.NGAY_CAP_NHAT)
                            , Data.CreateParameter("ACTIVE_FLAG", obj.ACTIVE_FLAG)
            );
        }

        /// <summary>
        /// Delete the specified GIA_DOAN_DUONG
        /// </summary>
        /// <param name="ma_gdd">MA_GDD</param>
        /// <returns></returns>
        public void Delete(Int64 ma_gdd)
        {
            SqlHelper.ExecuteNonQuery(Data.ConnectionString, CommandType.StoredProcedure, "sproc_GIA_DOAN_DUONG_Delete", Data.CreateParameter("MA_GDD", ma_gdd));
        }
        #endregion

    }
}
